/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UDPFiles;

import Tool.FileCodeMaker;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.sf.json.JSONObject;

/**
 * UDP文件保存类
 *
 * @author wcss
 */
public class UDPFileWriter {

    private File bufferFile = null;
    private File destFile = null;
    private HashMap<String, Boolean> dataIndexList = new HashMap<String, Boolean>();
    private int bufferSize = 2000;
    private FileChannel writeTempData = null;
    private ByteBuffer writeBuffer = null;

    public UDPFileWriter(File bufFile, File dstFile, int bufSize) throws Exception {
        this.bufferFile = bufFile;
        this.destFile = dstFile;
        this.bufferSize = bufSize;
        writeBuffer = ByteBuffer.allocate(500);
    }

    /**
     * 关闭文件
     *
     * @throws IOException
     */
    public void closeTemp() throws IOException {
        if (writeTempData != null) {
            writeTempData.force(true);
            writeTempData.close();
        }
    }

    /**
     * 保存文件到缓存
     */
    public void saveToBuffer(long dataIndex,byte[] data) throws Exception {
        long writePos = (dataIndex - 1) * 500;
        if (this.dataIndexList.containsKey(dataIndex + "")) {
            //数据已经写过了丢弃
            throw new Exception("data pack in buffer file!");
        } else {
            this.dataIndexList.put(dataIndex + "", true);
            writeBytes(writePos,data);
        }
    }

    /**
     * 写数据到缓冲
     * @param writePos
     * @param data
     * @throws Exception 
     */
    public void writeBytes(long writePos, byte[] data) throws Exception {
        writeTempData.position(writePos);
        writeBuffer.put(data);
        writeBuffer.flip();
        writeTempData.write(writeBuffer);
        writeBuffer.clear();
    }

    /**
     * 保存文件到缓存(立即)
     */
    public void flush() throws IOException {
        writeTempData.force(true);
    }

    /**
     * 初始化
     *
     * @param dataSize
     */
    public void init(long bufferFileLength) throws Exception {

        if (this.bufferFile.exists()) {
            this.bufferFile.delete();
        }

        RandomAccessFile raf = new RandomAccessFile(this.bufferFile, "rw");
        raf.setLength(bufferFileLength);
        raf.close();

        if (this.bufferFile != null) {
            writeTempData = new FileOutputStream(this.bufferFile).getChannel();
        }
    }

    /**
     * 生成目标数据文件
     *
     * @param datalength
     */
    public void makeDestFile(long dataPackCount) throws Exception {
        this.closeTemp();
        this.bufferFile.renameTo(this.destFile);
    }

    /**
     * 检查数据包是否有效
     *
     * @param jsons
     * @return
     * @throws Exception
     */
    public Boolean isUseful(String scode,byte[] databytes) throws Exception {
        String selfcode = FileCodeMaker.makeBytesCode(databytes);
        return scode.equals(selfcode);
    }

    /**
     * @return the bufferFile
     */
    public File getBufferFile() {
        return bufferFile;
    }

    /**
     * @return the destFile
     */
    public File getDestFile() {
        return destFile;
    }

    /**
     * @return the bufferSize
     */
    public int getBufferSize() {
        return bufferSize;
    }

    /**
     * @param bufferSize the bufferSize to set
     */
    public void setBufferSize(int bufferSize) {
        this.bufferSize = bufferSize;
    }
}
